package com.sxbbc.common.core.wx;

import com.sxbbc.common.basics.wx.WxrequestUrls;
import com.sxbbc.common.basics.wx.util.HttpUtil;
import com.sxbbc.common.basics.wx.util.TenpayUtil;
import com.sxbbc.common.core.entity.jz.InfoWx;
import com.sxbbc.common.core.utils.RedisUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.SortedMap;
import java.util.TreeMap;

public class GetRSAPublicKey {

    public static GetRSAPublicKey rsaKay = null;
    private static InfoWx infoWx = null;

    public static GetRSAPublicKey getInstance() {
        if (rsaKay == null) {
            synchronized (WxUtils.class) {
                if (rsaKay == null) {
                    rsaKay = new GetRSAPublicKey();
                }
            }
        }
        infoWx = RedisUtils.getInstance().getWxInfo();
        return rsaKay;
    }

    /**
     * 获取RSA加密的公钥,格式PKCS#1
     * @throws Exception
     */
    public void getPublicKey() throws Exception {
        //1.0 拼凑所需要传递的参数 map集合
        SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
        String nonce_str = TenpayUtil.CreateNoncestr();
        parameters.put("mch_id", infoWx.getMchId());
        parameters.put("nonce_str", nonce_str);
        parameters.put("sign_type", "MD5");
        //2.0 根据要传递的参数生成自己的签名~~注意creatSign是自己封装的一个类。大家可以在下面自主下载
        String sign = TenpayUtil.createSign("UTF-8", parameters, infoWx.getKeyval());
        //3.0将当前的map结合转化成xml格式
        String xmlData = TenpayUtil.getRequestXml_twice(parameters, sign);
        //4.0 发送请求到微信请求公钥Api。发送请求是一个方法来的~~注意需要带着证书哦
        String cert = HttpUtil.postCert(infoWx.getCerturl(), infoWx.getMchId(), xmlData, WxrequestUrls.WX_RSA_KEY);
        //5.0 解析返回的xml数据===》map集合
        Document dd = DocumentHelper.parseText(cert);
        if (dd != null) {
            Element root = dd.getRootElement();
            Element resultCode = root.element("result_code");
            if (resultCode != null) {
                String pub_key = resultCode.getText();
                System.out.println(pub_key);
            }
        }
    }

    public static void main(String[] args) {
        try {
            //1.0 拼凑所需要传递的参数 map集合
            SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
            String nonce_str = TenpayUtil.CreateNoncestr();
            parameters.put("mch_id", "1550561081");
            parameters.put("nonce_str", nonce_str);
            parameters.put("sign_type", "MD5");
            //2.0 根据要传递的参数生成自己的签名~~注意creatSign是自己封装的一个类。大家可以在下面自主下载
            String sign = TenpayUtil.createSign("UTF-8", parameters, "hOgxToZPKJnR5r4ZFkP5SWf3Tn0WNeO2");
            //3.0将当前的map结合转化成xml格式
            String xmlData = TenpayUtil.getRequestXml_twice(parameters, sign);
            //4.0 发送请求到微信请求公钥Api。发送请求是一个方法来的~~注意需要带着证书哦
            String cert = HttpUtil.postCert("C:/Users/Jane/Desktop/apiclient_cert.p12", "1550561081", xmlData, WxrequestUrls.WX_RSA_KEY);
            //5.0 解析返回的xml数据===》map集合
            Document dd = DocumentHelper.parseText(cert);
            if (dd != null) {
                Element root = dd.getRootElement();
                Element resultCode = root.element("result_code");
                if (resultCode != null) {
                    String pub_key = resultCode.getText();
                    System.out.println(pub_key);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
